﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class ZRendererAnim : AnimBase
{

    //float FloatLerpFunc(float a, float b, float value)
    //{
    //    return Mathf.Lerp(a, b, value);
    //}

    //public void AlphaAnim(Renderer renderer, int propertyID,float targetAlpha,float time, Action onEnd)
    //{
    //    StartCoroutine(PlayAnim<float>(targetAlpha, time, FloatLerpFunc,
    //      (alpha) =>
    //      {
    //          var color = renderer.material.GetColor(propertyID);
    //          color.a = alpha;
    //          renderer.material.SetColor(propertyID, color);
    //      },
    //      () =>
    //      {
    //          return renderer.material.GetColor(propertyID).a;
    //      }
    //     , onEnd));
    //}
    
    Color ColorLerpFunc(Color a, Color b, float value)
    {
        return Color.Lerp(a, b, value);
    }

    public Coroutine ColorAnim(Renderer renderer, int propertyID, Color targetColor, float time, Action onEnd)
    {
        return PlayAnim<Color>(targetColor, time, ColorLerpFunc,
          (color) =>
          {
              if(renderer)
                renderer.material.SetColor(propertyID, color);
          },
          () =>
          {
              if (!renderer || !renderer.material.HasProperty(propertyID))
              {
                  GameDebug.LogError($"hasProperty:{renderer.material.HasProperty(propertyID)}");
                  return Color.white;
              }
              
              return renderer.material.GetColor(propertyID);
          }
         , onEnd);
    }

    public Coroutine ColorAnim(Action<Color>setColorFunc,Func<Color> getColorFunc,  Color targetColor, float time, Action onEnd)
    {
        return PlayAnim<Color>(targetColor, time, ColorLerpFunc,
          (color) =>
          {
              if(setColorFunc != null)
                setColorFunc(color);
          },
          () =>
          {
              if (getColorFunc != null)
                  return getColorFunc();
              else
                  return Color.white;
          }
         , onEnd);
    }


    public void StopAnim()
    {
        StopAllCoroutines();
    }

}
